package com.qf.web.controller;

import com.qf.service.StuService;
import com.qf.service.impl.StuServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * projectName:lipengchong
 * author:LiPengChong
 * time:2024/4/30 16:36
 * description:
 */
@WebServlet("/deleteStu")
public class DeleteStuServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端数据
        String id = request.getParameter("id");
        //非空验证
        if (StringUtils.isEmpty(id)){
            response.sendRedirect("showStu");
            return;
        }
        try {
            //3.调用业务逻辑
            StuService stuService = new StuServiceImpl();
            stuService.delete(Integer.parseInt(id));
            //转发重定向
            response.sendRedirect("showStu.jsp");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg","出错了"+e.getMessage());
            request.getRequestDispatcher("message.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}


